package com.qf.Dao.impl;

import com.qf.Dao.SysRoleDao;
import com.qf.Utils.DBUtils;
import com.qf.entity.SysRole;
import com.qf.entity.SysUserRoleDept;
import org.apache.commons.dbutils.QueryRunner;
import org.apache.commons.dbutils.handlers.BeanHandler;

import java.sql.SQLException;

public class SysRoleDaoImpl implements SysRoleDao {
    private QueryRunner qr = new QueryRunner(DBUtils.DB.getDs());

    @Override
    public SysUserRoleDept selectName(String name) throws SQLException {
        String sql = "SELECT r.`name` as `名称`, d.simplename AS '所在部门',r.tips AS'别名'\n" +
                "FROM sys_user as u \n" +
                "LEFT  JOIN sys_role as r ON u.deptid = r.deptid \n" +
                "LEFT  JOIN sys_dept as d ON r.deptid= d.id\n" +
                "WHERE r.name = ?\n" +
                "LIMIT ?,?";
        Object[] objects = {name, 1, 1};
        SysUserRoleDept query = qr.query(sql, new BeanHandler<SysUserRoleDept>(SysUserRoleDept.class), objects);
        return query;
    }

    @Override
    public int addSysRole(SysRole sysRole) throws SQLException {
        String sql = "INSERT INTO sys_role(id,num,pid,name,deptid,tips,version)VALUES(?,?,?,?,?,?,?)";
        Object[] objects = {sysRole.getId(), sysRole.getNum(), sysRole.getPid(), sysRole.getName(), sysRole.getDeptid(), sysRole.getTips(), sysRole.getVersion()};
        return qr.update(sql, objects);
    }

    @Override
    public int updateSysRole(SysRole sysRole) throws SQLException {
        String sql = "UPDATE sys_role SET num=?,pid=?,name = ?,deptid = ?,tips = ?,version = ? WHERE id = ?";
        Object[] objects = {sysRole.getNum(), sysRole.getPid(), sysRole.getName(), sysRole.getDeptid(), sysRole.getTips(), sysRole.getVersion(), sysRole.getId()};
        return qr.update(sql, objects);
    }

    @Override
    public int deleteSysRole(String id) throws SQLException {
        String sql = "DELETE FROM sys_role WHERE id=?";
        return qr.update(sql, id);
    }

}
